package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;

    //分页查询
    @GetMapping("/page")
    public Result page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    //根据ID查询
    @GetMapping("/{id}")
    public Result queryById(@PathVariable Long id) {
        SetmealVO setmeal = setmealService.queryById(id);
        return Result.success(setmeal);
    }
//起售停售
    @CacheEvict(cacheNames = "setmealCache",allEntries = true )
    @PostMapping("status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        setmealService.updateStatus(status, id);
        return Result.success();
    }
    //新增添加套餐
    @CacheEvict(cacheNames = "setmealCache",key = "'setmeal_'+#a0.categoryId")
   @PostMapping
    public Result insert(@RequestBody SetmealDTO setmealDTO) {
        setmealService.insert(setmealDTO);
        return Result.success();

   }
   //批量删除
   @CacheEvict(cacheNames = "setmealCache",allEntries = true )
    @DeleteMapping
    public Result romoveBatch(@RequestParam List<Long> ids) {
        setmealService.deleteBatch(ids);
        return Result.success();
    }
    //修改套餐
    @CacheEvict(cacheNames = "setmealCache",allEntries = true )
    @PutMapping
    public Result update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }
}